﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.Collections;
using System.IO;
using Editor.Utilities;

namespace Editor.Old
{
    [XmlInclude(typeof(PageLabel)), XmlInclude(typeof(PageImageBox)), XmlInclude(typeof(PageHideButton))]
    public class Page : Element
    {
        private List<PageControl> pageControls = new List<PageControl>();
        private List<PageControl> dummyList = new List<PageControl>();

        public Page()
        {
        }

        public Page(string name)
            : base(name) 
        {
        }

        public override IList ChildElements
        {
            get
            {
                return dummyList;
            }
            set
            {
                dummyList = value as List<PageControl>;
            }
        }

        public List<PageControl> Controls
        {
            get
            {
                return pageControls;
            }
            set
            {
                pageControls = value;
            }
        }

        [XmlIgnore]
        public override string Path
        {
            get
            {
                string path = Name + ".pagx";

                for (Element parent = Parent; parent != null; parent = parent.Parent)
                    path = System.IO.Path.Combine(parent.Name, path);

                return path;
            }
        }

        [XmlIgnore]
        public bool IsLoaded { get; set; }

        [XmlIgnore]
        public bool IsSaved { get; set; }
        
        public override Element CreateChildElement(string childElementName)
        {
            return null;
        }

        public void AddPageControl(PageControl pageControl)
        {
            Controls.Add(pageControl);
            IsSaved = false;
        }

        public void RemovePageControl(PageControl pageControl)
        {
            Controls.Remove(pageControl);
            IsSaved = false;
        }

        public Page GetBasicPage()
        {
            return new Page(Name);
        }

        public Page Load()
        {
            Element parent = Parent;
            Page loadedPage = Load(Path);

            loadedPage.Parent = parent;

            return loadedPage;
        }

        public Page LoadBase(string basePath)
        {
            Element parent = Parent;
            Page loadedPage = Load(System.IO.Path.Combine(basePath, Path));

            loadedPage.Parent = parent;

            return loadedPage;
        }

        public static Page Load(string fileName)
        {
            Page page = XMLWorker<Page>.DeserializeFromXML(fileName);

            page.IsLoaded = true;
            page.IsSaved = true;

            return page;
        }

        public Page Copy(string path)
        {
            string realName = Name;
            string copyName = 
                path.Substring(path.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1).Replace(".pagx", string.Empty);
            
            Name = copyName;
            Save(path);
            Name = realName;

            return Load(path);
        }

        public void Save()
        {
            Save(Path);
        }

        public void Save(string path)
        {
            XMLWorker<Page>.SerializeToXML(this, path);
            IsSaved = true;
        }
    }
}
